package com.lb.shopguide.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.WxPayFailEvent;
import com.lb.shopguide.entity.WxPaySuccessEvent;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_result);

        api = WXAPIFactory.createWXAPI(this, AppConstant.WECHAT_APP_ID);
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        Toast.makeText(this, "onReq", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                Toast.makeText(this, "微信支付成功", Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(new WxPaySuccessEvent());
            } else {
                Toast.makeText(this, "微信支付失败", Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(new WxPayFailEvent());
            }
            finish();
        }
    }


//    private void sendRequestConfirmOrderPay(int mode) {
//        ApiOrder.getInstance().sendRequestConfirmOrderPay(resultBean.getOrderNum(), mode, listener, listener);
//    }
}